/**
 * 应用的核心js文件
 */
/*@ start to scor*/
grApp.controller('CenterCtrl',['$scope','$http','BaseService',function($scope,$http,BaseService){

    $scope.wx_inited = false;
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }else{
        onBridgeReady();
    }
    function onBridgeReady(){
        $scope.wx_inited = true;
        //alert('ok');
        //todo some wx things
    }

    $scope.jump_scor=function(args){
        location.href=(args);
    };
    $scope.jump_to_list_scor=function(args){
        location.href=('center/history');
    };
    var height = $(".height-width").width();
    $(".height-width").css("height",height );
    $(".height-width p").css("padding-top",height/4+"px" );
    $(".height-width-child").css("height",height );
    $(".height-width-child p").css("line-height",height+"px");

    $scope.getOrderByType=function(args){
        $http({
            method: 'GET',
            url: args,
        }).success(function (data, status, headers, config){
            $scope.orderlistres = data.listtype;
        }).error(function (data, status, headers, config){
            alert("查询异常！");
        });
    };

    $scope.load=function(ol){
        $scope.isqb='';
        $scope.isdfk='';
        $scope.isdsh='';
        $scope.isdpj='';

        switch (ol) {
            case '/user/nghistory/0':
                $scope.isqb='am-active';
                break;
            case '/user/nghistory/1':
                $scope.isdfk='am-active';
                break;
            case '/user/nghistory/3':
                $scope.isdsh='am-active';
                break;
            case '/user/nghistory/4':
                $scope.isdpj='am-active';
                break;
        }

        $http({
            method: 'GET',
            url: ol,
        }).success(function (data, status, headers, config){
            $scope.orderlistres = data.listtype;
        }).error(function (data, status, headers, config){
            alert("查询异常！");
        });
    };

    $scope.pinjia=function(args){
        //alert(args);
        location.href=('/user/eva/'+args);
    };

    $scope.getOrderInfo=function(args){
        alert('a');
        //location.href=(""+args);
    };

    //$scope.getUID=function(uid){
    //    $scope.yeid=uid;
    //}

    $scope.rechargeAmount = 100;//默认充值金额100元

    $scope.isActive = {i:true};
    $scope.chooseRecharge = function(amount,i){
        $scope.isActive = {};
        $scope.isActive[i] = true;
        $scope.rechargeAmount = amount;
    };

    $scope.doRecharge = function(){
        var timestamp = new Date().getTime();
        var orderid = 'C'+timestamp;

        //var uid = $scope.yeid;
        //var fee = parseFloat(args)*100;
        $http({
            methd:'GET',
            url:'/user/recharge/'+orderid+'/'+$scope.rechargeAmount,
        }).success(function(datas,status,headers,config){
            if(datas.code!=0){
                alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
                return;
            }
            //alert(datas.baocun);
            var fee = parseFloat($scope.rechargeAmount);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay(orderid,fee).then(function(data){
                    alert("噢耶~充值成功!快去个人中心看看吧~");
                    window.location.href='/user/center';
                }).catch(function(err){
                    alert(">_< ~ OMG! 就这么残忍的拒绝我了么?");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }).error(function(data, status, headers, config){
            alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
        });

    };

    $scope.chongZhi=function(args){

        var timestamp = new Date().getTime();
        var orderid = 'C'+timestamp;

        //var uid = $scope.yeid;

        //var fee = parseFloat(args)*100;
        $http({
            methd:'GET',
            url:'/user/recharge/'+orderid+'/'+args,
        }).success(function(datas,status,headers,config){
            //alert(datas.baocun);
            var fee = parseFloat(args);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay(orderid,fee).then(function(data){
                    alert("支付成功");
                    window.location.href='/user/center';
                }).catch(function(err){
                    alert("支付失败");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }).error(function(data, status, headers, config){
            alert("生成余额充值订单异常！");
        });

    };

    //判断用户是否绑定手机
    $scope.ifBingPhone=function(args){
        $http({
            methd:'GET',
            url:'/user/ifbindphone',
        }).success(function(data,status,headers,config){
            if(!data.bindphone) {
                //没有绑定手机，弹出模态框，进行绑定
                var value='';
                if('c'==args){
                    value = prompt("亲～绑定一下手机号，可以吗？");
                    //alert(value);
                }else if('m'==args){
                    value = prompt("请输入您要绑定的手机号：");
                }
                var re = /^1\d{10}$/;
                if(value !==null) {
                    if (re.test(value)) {
                        $scope.bindPhone(value);
                    } else {
                        alert('手机号码格式不对哦！');
                    }
                }
            }else{
                if('m'==args) {
                    alert('亲，您已经绑定过手机号啦！');
                }
            }
        }).error(function(data,status,headers,config){
            alert('网络连接异常！');
        });
    };
    //绑定手机
     $scope.bindPhone=function(args){
       $http({
           methd:'GET',
           url:'/user/bindphone/'+args,
       }).success(function(data, status, headers, config){
           alert('手机绑定成功，谢谢！');
       }).error(function(data, status, headers, config){
          alert('网络连接异常！');
       });
     };

    //去付款
    $scope.gotopay=function(args){
        //alert($scope.payid+args);
        if(args=='wx_pay'){
            var fee = parseFloat($scope.payfee);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay($scope.payid,fee).then(function(data){
                    alert("支付成功");
                    window.location.href='/user/history/1';
                }).catch(function(err){
                    alert("支付失败");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }else{
            $http({
                methd:'GET',
                url:'/order/againPay',
                params:{orderid:$scope.payid,payway:args},
            }).success(function(data, status, headers, config){
                alert("下单成功！");
                window.location.href='/user/history/1';
            }).error(function(data, status, headers, config){
                alert("下单失败！");
                window.location.href='/user/history/1';
            });
        }

    };

    //取消订单
    $scope.quxiao=function(args){
        //alert(args);
        //location.href=('/order/setOrderCancel/'+args);

        $http({
            methd:'GET',
            url:'/order/setOrderCancel/'+args,
            //params:{objectId:args},
        }).success(function(data, status, headers, config){
            alert("订单取消成功！");
            window.location.href='/user/history/1';
        }).error(function(data, status, headers, config){
            alert("订单取消失败！");
            window.location.href='/user/history/1';
        });

    };
    /*
    * 订单确认收货
    * */
    $scope.shouhuo=function(args){
        //alert(args);
        $http({
            methd:'GET',
            url:'/user/shouhuo/'+args,
            //params:{orderid:args},
        }).success(function(data, status, headers, config){
            alert("确认收货成功！");
            window.location.href='/user/history/3';
        }).error(function(data, status, headers, config){
            alert("确认收货失败！");
            window.location.href='/user/history/3';
        });
    };

    $scope.show = function(orderid,payfee){
        //alert(orderid);
        //alert(orderid);
        //alert(payfee);
        $scope.payid=orderid;
        $scope.payfee=payfee;
        $('#my-actions').modal();
    };

    $scope.chuli=function(args){
        args = JSON.parse(args);
        //alert(args[0].orderid);
        $scope.orderid=args[0].orderid;
        $scope.evagoods=args[0].goods;
        $scope.iconstart1='am-icon-star';
        $scope.iconstart2='am-icon-star';
        $scope.iconstart3='am-icon-star';
        $scope.iconstart4='am-icon-star';
        $scope.iconstart5='am-icon-star';
        $scope.starfor='好评';
    };

    $scope.clickstar=function(args){
        //alert(args);
        switch (args.charAt(0)) {
            case '1':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star-o';
                //$scope.iconstart3='am-icon-star-o';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';
                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('1星差评');
                //alert('#'+args.substr(1)+'star');
                break;
            case '2':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star-o';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('2星中评');
                break;
            case '3':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('3星中评');
                break;
            case '4':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('4星好评');
                break;
            case '5':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star';
                //$scope.iconstart5='am-icon-star';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star');

                $('#'+args.substr(1)+'star').text('5星好评');
                break;
        }
    };

    $scope.sendeva=function(args){
        //alert($scope.evagoods);
        var goods=$scope.evagoods;
        for(var i=0;i<goods.length;i++){
           //alert($('#'+goods[i].pid).val());
            var wenzi=$('#'+goods[i].pid).val();//评价内容
            var pjia=$('#'+goods[i].pid+'star').text() || '5';//星级

            //alert(pjia.charAt(0));
            $http({
                methd:'GET',
                url:'/user/saveeva/'+$scope.orderid+'/'+wenzi+'/'+goods[i].pid+'/'+goods[i].pname+'/'+pjia.charAt(0),
            }).success(function(data, status, headers, config){
                //alert("下单成功！");
            }).error(function(data, status, headers, config){
                //alert("下单失败！");
            });
        };
        alert('订单评价成功！');
        window.location.href='/user/history/4';

        //async.waterfall([
        //    function(callback){
        //        var res=0;
        //        var goods=$scope.evagoods;
        //        for(var i=0;i<goods.length;i++){
        //            //alert($('#'+goods[i].pid).val());
        //            var wenzi=$('#'+goods[i].pid).val();//评价内容
        //            var pjia=$('#'+goods[i].pid+'star').text();//星级
        //
        //            //alert(pjia.charAt(0));
        //            $http({
        //                methd:'GET',
        //                url:'/user/saveeva/'+$scope.orderid+'/'+wenzi+'/'+goods[i].pid+'/'+goods[i].pname+'/'+pjia.charAt(0),
        //            }).success(function(data, status, headers, config){
        //                res=0;
        //            }).error(function(data, status, headers, config){
        //                res=1;
        //            });
        //        }
        //
        //        callback(null,res);
        //    },
        //    function(res,callback){
        //        if(res==0){
        //            alert('订单评价成功！');
        //            window.location.href='/user/history/4';
        //        }
        //    }
        //],function(err,reeult){
        //    if(err){
        //        //TODO:
        //        return;
        //    }
        //    window.location.href='/user/history/4';
        //    //res.render('/user/history/4');
        //})
    };

    //判断用户是否已经设置支付密码
    $scope.ifsetpwd=function(args){
        $http({
            methd:'GET',
            url:'/user/ifsetpwd'
        }).success(function(data, status, headers, config){
            if(data.ifset){
                window.location.href='/user/password/xg';
            }else{
                window.location.href='/user/password/sz';
            }
        }).error(function(data, status, headers, config){

        });

    };
}]);
/*@ end to scor*/
